package com.sz.lk.wingsystem.ui;

import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sz.lk.wingsystem.R;
import com.sz.lk.wingsystem.bean.MenuBean;
import com.sz.lk.wingsystem.madapter.MenuAdapter;
import com.sz.lk.wingsystem.mutils.SaveUtils;
import com.sz.lk.wingsystem.mutils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author : liuke
 * e-mail : 1012459343@qq.com
 * date   : 2020/12/7
 * desc   :
 * version: 1.0
 */
public class MenuPage extends AppCompatActivity {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.menu_rcy)
    RecyclerView menuRcy;
    private MenuAdapter menuAdapter;
    private List<MenuBean> menuBeanList = new ArrayList<>();
    BluetoothAdapter bluetoothAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        setContentView(R.layout.activity_menu_page);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        back.setVisibility(View.INVISIBLE);
        MenuBean menuBean1 = new MenuBean();
        menuBean1.setDescribe("称重");
        menuBean1.setResource_id(R.drawable.lianjie);
        MenuBean menuBean2 = new MenuBean();
        menuBean2.setDescribe("校验");
        menuBean2.setResource_id(R.drawable.check);
        MenuBean menuBean3 = new MenuBean();
        menuBean3.setDescribe("退出");
        menuBean3.setResource_id(R.drawable.tuichu);
        menuBeanList.add(menuBean1);
        menuBeanList.add(menuBean2);
        menuBeanList.add(menuBean3);
        menuAdapter = new MenuAdapter(menuBeanList, this);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
        gridLayoutManager.setOrientation(GridLayoutManager.VERTICAL);
        menuAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM);
        menuAdapter.setOnItemClickListener((adapter, view, position) -> {
            ToastUtils.show(menuBeanList.get(position).getDescribe());
            switch (position) {
                case 0:
                    startActivity(new Intent(this, WeighPage.class));
                    break;
                case 1:
                    startActivity(new Intent(this, CheckPage.class));
                    break;
                case 2:
                  /*  try {
                        bluetoothAdapter.disable();
                        ToastUtils.show("蓝牙已关闭");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }*/
                    SaveUtils.getInstance().save(this, "login", "fail");
                    startActivity(new Intent(this, MainActivity.class));
                    finish();
                    break;
                default:
                    break;
            }
        });
        menuRcy.setLayoutManager(gridLayoutManager);
        menuRcy.setItemAnimator(new DefaultItemAnimator());
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this, DividerItemDecoration.HORIZONTAL);
        menuRcy.addItemDecoration(dividerItemDecoration);
        menuRcy.setAdapter(menuAdapter);
    }
}